var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./services/weixinService", "react", "components/common/base-component", "./services/localService", "json!websiteConfig", "maishu-jueying", "../components/component-container", "components/common/master-page"], factory);
    }
})(function (require, exports) {
    "use strict";
    var __syncRequire = typeof module === "object" && typeof module.exports === "object";
    Object.defineProperty(exports, "__esModule", { value: true });
    const weixinService_1 = require("./services/weixinService");
    const React = require("react");
    const base_component_1 = require("components/common/base-component");
    const localService_1 = require("./services/localService");
    const websiteConfig = require("json!websiteConfig");
    const maishu_jueying_1 = require("maishu-jueying");
    const component_container_1 = require("../components/component-container");
    const master_page_1 = require("components/common/master-page");
    maishu_jueying_1.Component.register(component_container_1.ComponentContainer.name, component_container_1.ComponentContainer);
    maishu_jueying_1.Component.register(master_page_1.MasterPage.name, master_page_1.MasterPage);
    let contextName = websiteConfig.stationPath;
    // import { MyComponentData } from "models";
    // export function defaultNavBar(element: HTMLElement,
    //     options?: { title?: string, right?: JSX.Element, showBackButton?: boolean });
    // export function defaultNavBar(elementPage: Page,
    //     options?: { title?: string, right?: JSX.Element, showBackButton?: boolean });
    function defaultNavBar(elementPage, options) {
        //=============================
        // window[ADMIN_APP] 表明为设计时
        // if (window[ADMIN_APP]) {
        //     return null;
        // }
        //=============================
        // let element: HTMLElement;
        // if ((elementPage as Page).element != null) {
        let element = elementPage.element;
        let app = elementPage.app;
        // }
        // else {
        //     element = elementPage;
        // }
        if (weixinService_1.isWeixin) {
            return weixinNavheader(element, options);
        }
        // document.title = options.title || "";
        options = options || {};
        let title = options.title || '';
        let className = element.className;
        if (className.indexOf("topbar-padding") < 0)
            element.className = className + ' topbar-padding';
        return (React.createElement("nav", { className: "bg-primary" },
            React.createElement("div", { className: "pull-left", style: { padding: 0, width: 60 } }, options.showBackButton == false ?
                React.createElement("span", { dangerouslySetInnerHTML: { __html: "&nbsp;" } }) :
                React.createElement("button", { name: "back-button", className: "left-button", style: { opacity: 1 }, onClick: () => app.back() },
                    React.createElement("i", { className: "icon-chevron-left" }))),
            React.createElement("div", { className: "pull-right", style: { padding: 0, width: 60 } }, options.right ? options.right :
                React.createElement("span", { dangerouslySetInnerHTML: { __html: "&nbsp;" } })),
            React.createElement("div", { className: "", style: { padding: 0 } },
                React.createElement("h4", null, title))));
    }
    exports.defaultNavBar = defaultNavBar;
    function weixinNavheader(elementPage, options) {
        let element;
        if (elementPage.element != null) {
            element = elementPage.element;
        }
        else {
            element = elementPage;
        }
        document.title = options.title || "";
        if (options.left == null && options.right == null)
            return null;
        let className = element.className;
        if (className.indexOf("topbar-padding") < 0)
            element.className = className + ' topbar-padding';
        return (React.createElement("nav", { className: "bg-primary" },
            React.createElement("div", { className: "col-xs-3", style: { padding: 0 } }, options.left),
            React.createElement("div", { className: "col-xs-6", style: { padding: 0 } }),
            React.createElement("div", { className: "col-xs-3", style: { padding: 0 } }, options.right ? (options.right) : null)));
    }
    /** 加载 pageData 中的组件 */
    function loadComponents(pageData) {
        return __awaiter(this, void 0, void 0, function* () {
            let componentInfos = [];
            let stack = [pageData];
            let componentPaths = yield getComponentInfos();
            while (stack.length > 0) {
                let item = stack.pop();
                if (item.type == "ComponentContainer") {
                    continue;
                }
                let componentPath = componentPaths.filter(o => o.name == item.type)[0];
                if (componentPath) {
                    componentInfos.push(componentPath);
                }
                (item.children || []).forEach(c => {
                    stack.push(c);
                });
            }
            return Promise.all(componentInfos.map(p => loadComponent(p)));
        });
    }
    exports.loadComponents = loadComponents;
    function loadComponent(componentInfo) {
        return new Promise((resolve, reject) => {
            // let componentInfos: ComponentInfo[] = [];
            requirejs({ context: contextName }, [`${componentInfo.path}`], (mod) => __awaiter(this, void 0, void 0, function* () {
                console.assert(componentInfo.name != null);
                console.assert(typeof mod[componentInfo.name] == "function");
                let { Component } = yield __syncRequire ? Promise.resolve().then(() => require("maishu-jueying")) : new Promise((resolve_1, reject_1) => { require(["maishu-jueying"], resolve_1, reject_1); });
                Component.register(componentInfo.name, mod[componentInfo.name]);
                resolve(componentInfos);
            }), (err) => {
                reject(err);
            });
        });
    }
    let componentInfos;
    function getComponentInfos() {
        return __awaiter(this, void 0, void 0, function* () {
            if (componentInfos != null && componentInfos.length > 0)
                return componentInfos;
            let localService = base_component_1.createService(localService_1.LocalService);
            componentInfos = yield localService.getComponentInfos();
            return componentInfos;
        });
    }
    function formatDate(date, showHourMinutes) {
        if (typeof date == "string")
            return date;
        let d = date;
        if (showHourMinutes)
            return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()} ${d.getHours() + 1}:${d.getMinutes()}`;
        return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`;
    }
    exports.formatDate = formatDate;
});
